/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alxbrendmaquinarias.controller.administration;

import com.alxbrendmaquinarias.model.Company;
import com.alxbrendmaquinarias.model.Shop;
import com.alxbrendmaquinarias.model.Storehouse;
import com.alxbrendmaquinarias.service.CompanyService;
import com.alxbrendmaquinarias.service.ShopService;
import com.alxbrendmaquinarias.service.StorehouseService;
import com.alxbrendmaquinarias.util.faces.FacesUtil;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 *
 * @author Max
 */
@Controller 
public class ShopRoleController implements Serializable {
    
    @Autowired
    ShopService shopService;
    @Autowired
    StorehouseService storehouseService;
    @Autowired
    CompanyService companyService;
    
    //////////////STOREHOUSE (ALMACÉN)//////////////
    private List<Storehouse> listStorehouse;
    private Storehouse storehouse = new Storehouse();
    ////////////////COMPANY////////////////////////
    private List<Company> listCompany;
    private Company company = new Company();
    
    public void initValuesStoreHouse() {
        listStorehouse = null;
        storehouse = new Storehouse();
    }
    
    public void initValuesCompany() {
        listCompany = null;
        company = new Company();
    }
    
    public void removeCompany(Company company) {
        try {
            companyService.remove(company);
            FacesUtil.addMessage("Eliminado", "Se ha eliminado un registro con éxito", FacesMessage.SEVERITY_INFO);
        } catch (Exception ex) {
            //Logger.getLogger(ShopRoleController.class.getName()).log(Level.SEVERE, null, ex);
            FacesUtil.addMessage("ERROR", "La empresa está siendo utilizado, no puede ser eliminado", FacesMessage.SEVERITY_ERROR);
        }
    }
    
    public void saveOrUpdateCompany() throws Exception {
        if(company.getId() == null) {
            companyService.save(company);
            FacesUtil.addMessage("Guardado", "Se ha añadido una empresa nueva", FacesMessage.SEVERITY_INFO);
            initValuesCompany();
        } else {
            companyService.update(company);
            FacesUtil.addMessage("Actualizado", "Se ha actualizado una empresa", FacesMessage.SEVERITY_INFO);
            initValuesCompany();
        }
        
    }
    
    public void saveOrUpdateStorehouse() throws Exception {
        if(storehouse.getId() == null) {
            storehouseService.save(storehouse);
            initValuesStoreHouse();
            FacesUtil.addMessage("Guardado", "Se ha añadido un nuevo almacén...", FacesMessage.SEVERITY_INFO);
        } else {
            storehouseService.update(storehouse);
            initValuesStoreHouse();
            FacesUtil.addMessage("Actualizado", "Se ha actualizado el almacén", FacesMessage.SEVERITY_INFO);
        }
    }
    
    public void removeStorehouse(Storehouse storehouse) {
        try {
            storehouseService.remove(storehouse);
            FacesUtil.addMessage("Eliminado", "Se ha eliminado el almacén seleccionado", FacesMessage.SEVERITY_INFO);
        } catch (Exception ex) {
            FacesUtil.addMessage("ERROR", "El almacén está siendo utilizado, no se puede eliminar.", FacesMessage.SEVERITY_ERROR);
            //Logger.getLogger(ShopRoleController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public List<Storehouse> getListStorehouse() throws Exception {
        if( listStorehouse == null) {
            listStorehouse = storehouseService.list("Storehouse.findAll");
        }
        return listStorehouse;
    }

    public List<Company> getListCompany() throws Exception {
        if(listCompany == null) {
            listCompany = companyService.list("Company.findAll");
        }
        return listCompany;
    }

    public void setListCompany(List<Company> listCompany) {
        this.listCompany = listCompany;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public void setListStorehouse(List<Storehouse> listStorehouse) {
        this.listStorehouse = listStorehouse;
    }

    public Storehouse getStorehouse() {
        return storehouse;
    }

    public void setStorehouse(Storehouse storehouse) {
        this.storehouse = storehouse;
    }
    
    
    ////////////SHOP (TIENDA)///////////////////////
    private List<Shop> listShop;
    private Shop shop = new Shop();
    public void initValuesShop() {
        listShop = null;
        shop = new Shop();
    }

    public void removeShop(Shop shop) {
        try {
            shopService.remove(shop);
            initValuesShop();
            FacesUtil.addMessage("Eliminado.", "La tienda fue eliminada", FacesMessage.SEVERITY_INFO);
        } catch (Exception ex) {
            FacesUtil.addMessage("ERROR", "La tienda tiene ventas!!!, no se puede eliminar", FacesMessage.SEVERITY_ERROR);
            Logger.getLogger(ShopRoleController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void saveOrUpdateShop() throws Exception {
        if(shop.getId() == null){
            shopService.save(shop);
            initValuesShop();
            FacesUtil.addMessage("Guardado", "Se ha añadio una nueva tienda...", FacesMessage.SEVERITY_INFO);
        } else {
            shopService.update(shop);
            initValuesShop();
            FacesUtil.addMessage("Actualizado", "Se ha actualizado una tienda...", FacesMessage.SEVERITY_INFO);
        }
    }
    
    public List<Shop> getListShop() throws Exception {
        if(listShop == null) {
            listShop = shopService.list("Shop.findAll");
        }
        return listShop;
    }

    public void setListShop(List<Shop> listShop) {
        this.listShop = listShop;
    }

    public Shop getShop() {
        return shop;
    }

    public void setShop(Shop shop) {
        this.shop = shop;
    }
    
    
    
}
